//
//  AccountViewModel.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/17.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit
import AFNetworking

class AccountViewModel: NSObject {

//    定义模型对象的属性
    var userAccount : UserAccount?
    
   override init(){
    
    userAccount = UserAccount.loadAccount()
    super.init()
    
    }
    
    
//    计算性属性，看用户是否登录
    var userLogin : Bool  { 
    
    return userAccount?.access_token != nil
    
    }
    
    
//   设置图片的URL
    var heardUrl :NSURL?{

        let url = NSURL(string: userAccount?.avatar_large ?? "")
return url
    
    }
//   获取用户的name
    var userName:String? {
    
    return userAccount?.name
        
    }
    
//    获取用户的access_token
    var access_token:String? {
        
    return userAccount?.access_token
    
    }
//    获取token
    
    func loadAccessToken(code:String, finished :(isSuccess : Bool)->()){
        
        let manager = AFHTTPSessionManager()
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        //        解决大坑,添加解析json的数据类型
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        manager.POST(urlString, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_,result) -> Void in
                print(result)
                
                //                字典转模型
                if let dic  = result as? [String:AnyObject]{
                    
                    let userAccount = UserAccount(dic:dic)
                    
                    self.loadUserInfo(userAccount,finished: finished)
                    
                }
                
                
            }) { (_, error) -> Void in
                print(error)
                finished(isSuccess: false)
        }
    }
    
    
//   获取用户信息
    private   func loadUserInfo (account:UserAccount,finished:(isSuccess:Bool)->()){
        
        let AFN = AFHTTPSessionManager()
        
        //        获取用户信息的url
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        
        
        if let token = account.access_token,uid = account.uid{
            //        需要的必须参数
            let parameters = ["access_token":token,"uid":uid]
            AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
            AFN.GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
                    //            print(result)
                    
                    //            给字典赋值
                    if let dic = result as? [String:AnyObject]{
                        
                        let avatar_large = dic["avatar_large"]as?String
                        let name = dic["name"] as?String
                        
                        //     给account对象做赋值操作
                        account.avatar_large = avatar_large
                        account.name = name
                        
                        //                        将数据保存沙盒
                        account.saveAccount()
                        
//                        在此处回传一个true
                       finished (isSuccess: true)
                        
                    }
                    
                }) { (_, error) -> Void in
                    print(error)
                    finished(isSuccess: false)
                    
            }
            
        }
        
    }

}
